// packageA/pages/choujiang4/choujiang4.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appurl: app.apiurl,
    apiimg: app.apiimg,
    autoplay: true,
    interval: 2000,
    duration: 500,
    array: ['请选择', '男', '女'],
    index: 0,
    idx: 0,
    idx1: 0,
    activity_id: '',
    prize_id: '',
    baoming2: false,
    baoming3: false,
    page_no: 1,
    page_no1: 1,
    activity_id: '',
    bm_status: '0',
    cuowu: false,
    days: 0,
    hours: 0,
    minutes: 0,
    seconds: 0,
    endTime: '2024/03/01 00:00:00', // 使用兼容的日期字符串格式
    prize_id: '',
    bm_id: 0,
    timer: '',
    hj_record: []
  },
  previewSqs(event) {
    // // 拿到图片的地址url
    // let currentUrl = event.currentTarget.dataset.src;
    // // 微信预览图片的方法
    // wx.previewImage({
    //     current: currentUrl, // 图片的地址url
    //     urls: [this.data.idx_gg.tj_activity.pic] // 预览的地址url
    // })
    wx.navigateTo({
      url: '/pages/yaoqing/yaoqing',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      activity_id: options.id
    })
    this.hjjilu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.z_idxgg()
    this.z_yonghu()
    // this.z_kaihei()
    this.z_jilu()
    this.z_jilu3()

    // this.z_jilu4()
  },
  hjjilu() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token")
    }
    wx.request({
      url: app.apiurl + '/api/member_kh/hjRecordList?page_no=1&page_num=10&prize_type=2',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          hj_record: res.data.data
        })
      }
    })
  },
  z_jilu3() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token")
    }
    wx.request({
      url: app.apiurl + 'api/config/unprizeList?fbwz_type=3',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          mylist: res.data.data
        })
      }
    })
  },
  z_jilu4() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token")
    }
    wx.request({
      url: app.apiurl + 'api/member_tj/tjBmStatus',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          bm_status: res.data.data.bm_status
        })
      }
    })
  },
  z_idxgg() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/home/index',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          login: true,
          idx_gg: res.data.data,
          // time1:res.data.data.activity_info.diff_time
        })
        // _this.z_time()
      }
    })
  },
  z_yonghu() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/member/myInfo',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        if (res.data.code == '1001') {
          _this.setData({
            login: true,
            yonghu: res.data.data
          })
        }
        if (res.data.data.yd_status == 5) {
          _this.z_yindao()
        }
      }
    })
  },
  z_yindao() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
      "yd_status": '6'
    }
    wx.request({
      url: app.apiurl + 'api/member/upMyYd',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {}
    })
  },
  z_kaihei() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/member/tjRecord?page_no=' + _this.data.page_no + '&page_num=10',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          list: res.data.data.list
        })
      }
    })
  },
  z_jilu() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token")
    }
    wx.request({
      url: app.apiurl + 'api/member_tj/getTjActivityDetail?activity_id=' + _this.data.activity_id,
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          list3: res.data.data,
          bm_status: res.data.data.ybm_status,
          bm_id: res.data.data.bm_id,
        })
        if (Number(res.data.data.diff_time) > 0) {
          _this.countdown2(res.data.data.diff_time)
        }
      }
    })
  },
  countdown2: function (difftime1) {
    const that = this;
    let difftime = Number(difftime1);
    const interval = setInterval(function () {
      if (difftime > 0) {
        difftime--;
        var seconds = difftime;
        var days = Math.floor(seconds / (3600*24));
        seconds %= 3600*24;
     
        var hours = Math.floor(seconds / 3600);
        seconds %= 3600;
     
        var minutes = Math.floor(seconds / 60);
        seconds %= 60;
   
        console.log('11111',difftime,seconds)
        that.setData({
          days: days,
          hours: hours,
          minutes: minutes,
          seconds: seconds,
        })
      } else {
        clearInterval(interval);
        that.z_jilu();
        // 倒计时结束的操作
      }
    }, 1000);
  },
  countDown: function (difftime) {
    const that = this;
    let diff_time = Number(difftime) * 1000;
    var endTime = new Date().getTime() + diff_time;
    that.data.timer = setInterval(function () {
      const nowTime = new Date().getTime();
      // const endTime = new Date(that.data.endTime.replace(/-/g, '/')).getTime(); 
      let distance = endTime - nowTime;
      const days = Math.floor(distance / (1000 * 60 * 60 * 24));
      const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      const seconds = Math.floor((distance % (1000 * 60)) / 1000);
      if (days < 0) {
        days = 0;
      }
      if (hours < 0) {
        hours = 0;
      }
      if (minutes < 0) {
        minutes = 0;
      }
      if (seconds < 0) {
        seconds = 0;
      }
      that.setData({
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds,
      });
    }, 1000);
  },

  z_open1() {
    this.setData({
      baoming2: true
    })
  },
  z_open2() {
    this.setData({
      baoming3: true
    })
  },
  z_close() {
    this.setData({
      baoming2: false,
      baoming3: false
    })
  },
  z_close1() {
    this.setData({
      cuowu: false
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  z_baoming1() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
      "activity_id": _this.data.list3.id,
      "prize_id": _this.data.list3.prize_id
    }
    if (_this.data.idx == '-1') {
      wx.showToast({
        title: '请选择奖品',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.request({
        url: app.apiurl + 'api/member_tj/tjhdBm',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          if (res.data.code = '1002') {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          } else {
            //  _this.setData({
            //    cuowu:true
            //  })
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
          _this.z_close()
          _this.z_idxgg()
          _this.z_jilu()
          //  _this.z_jilu4()
        }
      })
    }
  },
  z_baoming2() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
      "bm_id": _this.data.bm_id
    }
    wx.request({
      url: app.apiurl + 'api/member_tj/tjhdQxBm',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
        _this.z_close()
        _this.z_idxgg()
        _this.z_jilu()
        // _this.z_jilu4()
        _this.setData({
          baoming3: false
        })
      }
    })
  },
  z_baoming3(e) {
    // this.setData({
    //   idx:e.currentTarget.dataset.index,
    //   prize_id:e.currentTarget.dataset.id
    // })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    //下滑刷新
    //  let _this = this
    //  let page_no  = _this.data.page_no
    //  page_no = page_no+1
    //  let page_no1  = _this.data.page_no1
    //  page_no1 = page_no1+1
    //  _this.setData({
    //    page_no:page_no,
    //    page_no1:page_no1
    //  })
    //  _this.z_kaihei()
    //  _this.z_jilu()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})